<?php

namespace Home\Controller;

/**
 * 打卡
 *
 * @author Administrator
 *        
 */
class ClockInController extends BaseController {
	/**
	 * 打卡上班
	 */
	public function mb_ClockIn() {
		$data = $this->mb_OnMessage ();
		$token = $data->token;
		$jobNum = $data->jobnum;
		
		$ret = $this->mb_CheckAdminIsLoggedIn ( $token );
		$adminInfo = $ret->info;
		$adminData = $ret->data;
		
		if ($adminData ['jobnum'] != '') {
			$this->mb_OutMessage ( ErrCode_Toast, '您已上班，请勿重复上班' );
			return;
		}
		$id = $adminData ['id'];
		$adminInfo->save ( array (
				'id' => $id,
				'clockin' => \Tools::mb_GetCurDateTime (),
				'jobnum' => $jobNum 
		) );
		$this->mb_OutMessage ( ErrCode_Normal, ErrMsg_Normal );
	}
	/**
	 * 打卡下班
	 */
	public function mb_ClockOut() {
		$data = $this->mb_OnMessage ();
		$token = $data->token;
		
		$ret = $this->mb_CheckAdminIsLoggedIn ( $token );
		$adminInfo = $ret->info;
		$adminData = $ret->data;
		
		if ($adminData ['jobnum'] == '') {
			$this->mb_OutMessage ( ErrCode_Toast, '您未上班，请先上班' );
			return;
		}
		
		$handlerName = $adminData ['name'];
		$date1 = $adminData ['clockin'];
		$date2 = \Tools::mb_GetCurDateTime ();
		
		$userRechargeLog = M ( 'Userrechargelog' );
		$up = $userRechargeLog->where ( "handlername='$handlerName' AND date>='$date1' AND date<'$date2' AND type='0'" )->sum ( 'coins' );
		$down = $userRechargeLog->where ( "handlername='$handlerName' AND date>='$date1' AND date<'$date2' AND type='1'" )->sum ( 'coins' );
		$send = $userRechargeLog->where ( "handlername='$handlerName' AND date>='$date1' AND date<'$date2' AND type='2'" )->sum ( 'coins' );
		
		if ($up == '')
			$up = 0;
		else
			round ( $up, 2 );
		
		if ($down == '')
			$down = 0;
		else
			round ( $down, 2 );
		
		if ($send == '')
			$send = 0;
		else
			round ( $send, 2 );
		
		$id = $adminData ['id'];
		$name = $adminData ['name'];
		$jobNum = $adminData ['jobnum'];
		
		$clockInLog = M ( 'Clockinlog' );
		$clockInLog->add ( array (
				'adminname' => $name,
				'jobnum' => $jobNum,
				'clockin' => $date1,
				'clockout' => $date2,
				'up' => $up,
				'down' => $down,
				'send' => $send 
		) );
		
		$adminInfo->save ( array (
				'id' => $id,
				'clockin' => '',
				'clockout' => '',
				'jobnum' => '' 
		) );
		$this->mb_OutMessage ( ErrCode_Normal, ErrMsg_Normal );
	}
	/**
	 * 获取打卡数据
	 */
	public function mb_GetClockInData() {
		// $data = $this->mb_OnMessage ();
		// $token = $data->token;
		// $jobNum = $data->jobnum;
		
		// $ret = $this->mb_CheckAdminIsLoggedIn ( $token );
		// $adminInfo = $ret->info;
		// $adminData = $ret->data;
		
		// $adminName = $adminData ['name'];
		// $clockInLog = M ( 'Clockinlog' );
		
		// // if ($adminData ['level'] == 0) {
		// // $aName = C ( 'ADMIN_NAME' );
		// // $dName = C ( 'DEVE_NAME' );
		
		// // if ($adminData ['name'] == $aName) {
		// // $adminDatas = $adminInfo->where ( "level='0'" )->select ();
		// // } else if ($adminData ['name'] == $dName) {
		// // $adminDatas = $adminInfo->where ( "level='0' AND name!='$aName'" )->select ();
		// // }
		// // } else if ($adminData ['level'] == 1) {
		// // $code = $adminData ['code'];
		// // $subCode = mb_substr ( $code, 0, 5 );
		// // $adminDatas = $adminInfo->where ( "code like '" . $subCode . "___'" )->select ();
		// // } else if ($adminData ['level'] == 2) {
		// // $code = $adminData ['code'];
		// // $adminDatas = $adminInfo->where ( "code='$code'" )->select ();
		// // }
		
		// // $cData = array ();
		// // for($i = 0; $i < count ( $adminDatas ); $i ++) {
		// // $adminName = $adminDatas [$i];
		// // if ($jobNum == '')
		// // $clockInData = $clockInLog->where ( "adminname='$adminName'" )->order ( 'id desc' )->select ();
		// // else
		// // $clockInData = $clockInLog->where ( "adminname='$adminName' AND jobnum='$jobNum'" )->order ( 'id desc' )->select ();
		// // $cData = array_merge ( $cData, $clockInData );
		// // }
		// if ($jobNum == '')
		// $clockInData = $clockInLog->where ( "adminname='$adminName'" )->order ( 'id desc' )->select ();
		// else
		// $clockInData = $clockInLog->where ( "adminname='$adminName' AND jobnum='$jobNum'" )->order ( 'id desc' )->select ();
		
		// $this->mb_OutMessage ( ErrCode_Normal, json_encode ( $clockInData ) );
		$data = $this->mb_OnMessage ();
		$token = $data->token;
		$jobNum = $data->jobnum;
		
		$ret = $this->mb_CheckAdminIsLoggedIn ( $token );
		$adminInfo = $ret->info;
		$adminData = $ret->data;
		
		$adminName = $adminData ['name'];
		$clockInLog = M ( 'Clockinlog' );
		if ($jobNum == '')
			$clockInData = $clockInLog->where ( "adminname='$adminName'" )->order ( 'id desc' )->select ();
		else
			$clockInData = $clockInLog->where ( "adminname='$adminName' AND jobnum='$jobNum'" )->order ( 'id desc' )->select ();
		
		$this->mb_OutMessage ( ErrCode_Normal, json_encode ( $clockInData ) );
	}
	/**
	 * 获取打卡详情
	 */
	public function mb_GetClockInDetail() {
		$data = $this->mb_OnMessage ();
		$token = $data->token;
		$name = $data->name;
		$date1 = $data->date1;
		$date2 = $data->date2;
		
		$ret = $this->mb_CheckAdminIsLoggedIn ( $token );
		$adminInfo = $ret->info;
		$adminData = $ret->data;
		
		$handlerName = $name;
		$userRechargeLog = M ( 'Userrechargelog' );
		
		if($date2 == '-'){
			$userRechargeData = $userRechargeLog->where ( "date>='$date1' AND handlername='$handlerName'" )->select ();
		}else {
			$userRechargeData = $userRechargeLog->where ( "date>='$date1' AND date<'$date2' AND handlername='$handlerName'" )->select ();
		}
		
		$this->mb_OutMessage ( ErrCode_Normal, json_encode ( $userRechargeData ) );
	}
	/**
	 * 按时间查询打卡信息
	 */
	public function mb_GetClockInDataForDate() {
		$data = $this->mb_OnMessage ();
		$token = $data->token;
		
		$date1 = $data->date1;
		$date2 = $data->date2;
		
		$ret = $this->mb_CheckAdminIsLoggedIn ( $token );
		$adminInfo = $ret->info;
		$adminData = $ret->data;
		
		if ($adminData ['level'] == 0) {
			$aName = C ( 'ADMIN_NAME' );
			$dName = C ( 'DEVE_NAME' );
			
			if ($adminData ['name'] == $aName) {
				$adminDatas = $adminInfo->where ( "level='0' OR level='1'" )->select ();
			} else if ($adminData ['name'] == $dName) {
				$adminDatas = $adminInfo->where ( "(level='0' OR level='1') AND name!='$aName'" )->select ();
			}
		} else if ($adminData ['level'] == 1) {
			$adminName = $adminData ['name'];
			$adminDatas = $adminInfo->where ( "name = '$adminName'" )->select ();
		} else if ($adminData ['level'] == 2) {
			$code = $adminData ['code'];
			$subCode = mb_substr ( $code, 0, 5 );
			$adminDatas = $adminInfo->where ( "code like '" . $subCode . "___'" )->select ();
		} else if ($adminData ['level'] == 3) {
			$code = $adminData ['code'];
			$adminDatas = $adminInfo->where ( "code='$code'" )->select ();
		}
		
		$cData = array ();
		$clockInLog = M ( 'Clockinlog' );
		for($i = 0; $i < count ( $adminDatas ); $i ++) {
			$adminName = $adminDatas [$i] ['name'];
			$clockInData = $clockInLog->where ( "adminname='$adminName' AND clockin>='$date1' AND clockout<='$date2'" )->order ( 'id desc' )->select ();
			$cData = array_merge ( $cData, $clockInData );
		}
		$this->mb_OutMessage ( ErrCode_Normal, json_encode ( $cData ) );
	}
	/**
	 */
	public function mb_GetClockInRecord1() {
		$data = $this->mb_OnMessage ();
		$token = $data->token;
		
		$ret = $this->mb_CheckAdminIsLoggedIn ( $token );
		$adminInfo = $ret->info;
		$adminData = $ret->data;
		
		if ($adminData ['level'] == 0) {
			$aName = C ( 'ADMIN_NAME' );
			$dName = C ( 'DEVE_NAME' );
			$mName = C('MANA_NAME');
			if ($adminData ['name'] == $aName) {
				$adminDatas = $adminInfo->where ( "level='0' OR level='1'" )->select ();
			} else if ($adminData ['name'] == $dName) {
				$adminDatas = $adminInfo->where ( "(level='0' OR level='1') AND name!='$aName'" )->select ();
			}else  if ($adminData ['name'] == $dName) {
				$adminDatas = $adminInfo->where ( "(level='0' OR level='1') AND name!='$aName' AND name!=$dName" )->select ();
			}
		} else if ($adminData ['level'] == 1) {
			$adminName = $adminData ['name'];
			$adminDatas = $adminInfo->where ( "name = '$adminName'" )->select ();
		} else if ($adminData ['level'] == 2) {
			$code = $adminData ['code'];
			$subCode = mb_substr ( $code, 0, 5 );
			$adminDatas = $adminInfo->where ( "code like '" . $subCode . "___'" )->select ();
		} else if ($adminData ['level'] == 3) {
			$code = $adminData ['code'];
			$adminDatas = $adminInfo->where ( "code='$code'" )->select ();
		}
		
		$cData = array ();
		for($i = 0; $i < count ( $adminDatas ); $i ++) {
			$adminName = $adminDatas [$i] ['name'];
			$adminCode = $adminDatas [$i] ['code'];
			
			$userRechargeLog = M ( 'Userrechargelog' );
			$up = $userRechargeLog->where ( "handlername = '$adminName'  AND type='0'" )->sum ( 'coins' );
			$down = $userRechargeLog->where ( "handlername = '$adminName' AND type='1'" )->sum ( 'coins' );
			$send = $userRechargeLog->where ( "handlername = '$adminName' AND type='2'" )->sum ( 'coins' );
			
			if ($up == '')
				$up = 0;
			else
				$up = round ( $up, 2 );
			if ($down == '')
				$down = 0;
			else
				$down = round ( $down, 2 );
			if ($send == '')
				$send = 0;
			else
				$send = round ( $send, 2 );
			
			$adminJobNum = $adminDatas [$i] ['jobnum'];
			$adminClockIn = $adminDatas [$i] ['clockin'];
			if ($adminJobNum != null && $adminJobNum != '') {
				
				$lastClockIn = $adminClockIn;
				$status = '上班中';
			} else {
				$clockInLog = M ( 'Clockinlog' );
				$clockInData = $clockInLog->where ( "adminname='$adminName'" )->order ( 'id desc' )->find ();
				$lastClockIn = $clockInData ['clockout'];
				$status = '已下班';
			}
			
			$cData [] = array (
					'name' => $adminName,
					'code' => $adminCode,
					'up' => $up,
					'down' => $down,
					'send' => $send,
					'last' => $lastClockIn,
					'status' => $status 
			);
		}
		$this->mb_OutMessage ( ErrCode_Normal, json_encode ( $cData ) );
	}
	/**
	 */
	public function mb_GetClockInRecord2() {
		$data = $this->mb_OnMessage ();
		$token = $data->token;
		$agentName = $data->name;
		
		$ret = $this->mb_CheckAdminIsLoggedIn ( $token );
		$adminInfo = $ret->info;
		$adminData = $ret->data;
		
		$agentData = $adminInfo->where ( "name='$agentName'" )->find ();
		$agentCode = $agentData ['code'];
		$cData = array ();
		if ($agentData ['jobnum'] != null && $agentData ['jobnum'] != '') {
			$date = $agentData ['clockin'];
			
			$userRechargeLog = M ( 'Userrechargelog' );
			$up = $userRechargeLog->where ( "handlername = '$agentName' AND date>='$date' AND type='0'" )->sum ( 'coins' );
			$down = $userRechargeLog->where ( "handlername = '$agentName' AND date>='$date' AND type='1'" )->sum ( 'coins' );
			$send = $userRechargeLog->where ( "handlername = '$agentName' AND date>='$date' AND type='2'" )->sum ( 'coins' );
			
			if ($up == '')
				$up = 0;
			else
				$up = round ( $up, 2 );
			if ($down == '')
				$down = 0;
			else
				$down = round ( $down, 2 );
			if ($send == '')
				$send = 0;
			else
				$send = round ( $send, 2 );
			
			$tmp = array (
					'id' => 'tmp_1',
					'name' => $agentName,
					'code' => $agentCode,
					'jobnum' => $agentData ['jobnum'],
					'clockin' => $agentData ['clockin'],
					'clockout' => '-',
					'up' => ( string ) $up,
					'down' => ( string ) $down,
					'send' => ( string ) $send 
			);
			$tmpArr = array ();
			$tmpArr [] = $tmp;
			$cData = array_merge ( $cData, $tmpArr );
		}
		
		$clockInLog = M ( 'Clockinlog' );
		$clockInData = $clockInLog->where ( "adminname='$agentName'" )->order ( 'id desc' )->select ();
		
		for($i = 0; $i < count ( $clockInData ); $i ++) {
			$clockInData [$i] ['name'] = $clockInData [$i] ['adminname'];
			$clockInData [$i] ['code'] = $agentCode;
		}
		
		$cData = array_merge ( $cData, $clockInData );
		
		$this->mb_OutMessage ( ErrCode_Normal, json_encode ( $cData ) );
	}
}